/**
 * 设置缓存数据
 * @param {String} key - 键名
 * @param {Any} value - 键值
 * @param {String} [type=session] - 缓存类型
 * @returns {Void}
 */
export const setStorage = (key, value, type = "session") => {
  if (type === "session") {
    setSession("$" + key, value);
  } else {
    setLocal("$" + key, value);
  }
};
/**
 * 获取缓存数据
 * @param {String} key - 键名
 * @param {String} [type=session] - 缓存类型
 * @returns {Any} - 键值
 */
export const getStorage = (key, type = "session") => {
  if (type === "session") {
    return getSession("$" + key);
  } else {
    return getLocal("$" + key);
  }
};
/**
 * 移除缓存数据
 * @param {String} key - 键名
 * @param {String} [type=session] - 缓存数据
 * @returns {Void}
 */
export const removeStorage = (key, type = "session") => {
  if (type === "session") {
    sessionStorage.removeItem("$" + key);
  } else {
    localStorage.removeItem("$" + key);
  }
};

/**
 * 设置sessionStorage的缓存数据
 * @param {String} key - 键名
 * @param {Any} value - 键值
 * @returns {Void}
 */
export const setSession = (key, value) => {
  sessionStorage.removeItem(key);
  sessionStorage.setItem(key, JSON.stringify(value));
};
/**
 * 获取sessionStorage的缓存数据
 * @param {String} key - 键名
 * @returns {Any} - 键值
 */
export const getSession = key => {
  const value = sessionStorage.getItem(key);
  return value == "undefined" ? "" : JSON.parse(value);
};

/**
 * 设置localStorage缓存数据
 * @param {String} key - 键名
 * @param {Any} value - 键值
 * @returns {Void}
 */
export const setLocal = (key, value) => {
  localStorage.removeItem(key);
  localStorage.setItem(key, JSON.stringify(value));
};
/**
 * 获取localStorage缓存数据
 * @param {String} key - 键名
 * @returns {Any} - 键值
 */
export const getLocal = key => {
  const value = localStorage.getItem(key);
  return value == "undefined" ? "" : JSON.parse(value);
};
